<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" import="org.apak.model.*" import="org.apak.control.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert device</title>
</head>
<body>
<%	
	//Device[] deviceArray = (Device[]) request.getAttribute("deviceArray");
	DeviceType[] deviceTypeArray = (DeviceType[]) request.getAttribute("deviceTypeArray");
	ErrorList errors = (ErrorList) request.getAttribute("errors");
	String success = (String) request.getAttribute("success");
%>
<h2>Insert device</h2>
<%
	if(deviceTypeArray != null) {
		if(errors != null) {
			DeviceForm deviceForm = (DeviceForm)errors.getForm();
%>
<form action='deviceInsert?action=save' method='POST'>
	<table>
		<tr>
			<td>Name</td>
			<td><input type="text" name="name" value="<%= deviceForm.getName() %>"><%= errors.getError("name") %></td>
		</tr>
		<tr>
			<td>Model</td>
			<td><input type="text" name="model"></td>
		</tr>
		<tr>
			<td>Description</td>
			<td><input type="text" name="description"></td>
		</tr>
		<tr>
			<td>Manufacturer</td>
			<td><input type="text" name="manufacturer"></td>
		</tr>
		<tr>
			<td>Serial number</td>
			<td><input type="text" name="reg_no"></td>
		</tr>
		<tr>
			<td>Device type</td>
			<td>
				<select name="device_type">
		<%
					int j;
					for(j = 0; j < deviceTypeArray.length; j++) {
						DeviceType deviceType = deviceTypeArray[j];
						//deviceType.getDevice_type() == type_id &&
						if(deviceType.getLevel() == 1) {
		%>
							<option value="<%= deviceTypeArray[j].getDevice_type() %>" disabled><%= deviceTypeArray[j].getType_name() %></option>
		<%	
						} else if(deviceType.getLevel() == 2 && deviceType.getDevice_type() == Integer.parseInt(deviceForm.getDevice_type_fk())){
		%>
								<option value="<%= deviceTypeArray[j].getDevice_type() %>" selected>...<%= deviceTypeArray[j].getType_name() %></option>	
		<%	
						} else if(deviceType.getLevel() == 2){
		%>
								<option value="<%= deviceTypeArray[j].getDevice_type() %>">...<%= deviceTypeArray[j].getType_name() %></option>	
		<%	
						}
					}
		%>
				</select>
			</td>
		</tr>
	</table>
	<input type="submit" value="Insert"/>
</form>
<%
		}
		else if(success != null) {
%>
<form action='deviceInsert?action=save' method='POST'>
	<table>
		<tr>
			<td>Name</td>
			<td><input type="text" name="name"></td>
		</tr>
		<tr>
			<td>Model</td>
			<td><input type="text" name="model"></td>
		</tr>
		<tr>
			<td>Description</td>
			<td><input type="text" name="description"></td>
		</tr>
		<tr>
			<td>Manufacturer</td>
			<td><input type="text" name="manufacturer"></td>
		</tr>
		<tr>
			<td>Serial number</td>
			<td><input type="text" name="reg_no"></td>
		</tr>
		<tr>
			<td>Device type</td>
			<td>
				<select name="device_type">
		<%
					int j;
					for(j = 0; j < deviceTypeArray.length; j++) {
						DeviceType deviceType = deviceTypeArray[j];
						//deviceType.getDevice_type() == type_id &&
						if(deviceType.getLevel() == 1) {
		%>
							<option value="<%= deviceTypeArray[j].getDevice_type() %>" disabled><%= deviceTypeArray[j].getType_name() %></option>
		<%	
						} else if(deviceType.getLevel() == 2){
		%>
								<option value="<%= deviceTypeArray[j].getDevice_type() %>">...<%= deviceTypeArray[j].getType_name() %></option>	
		<%	
						}
					}
		%>
				</select>
			</td>
		</tr>
	</table>
	<input type="submit" value="Insert"/> <%= success %>
</form>
		<%
		}
		else {
%>
<form action='deviceInsert?action=save' method='POST'>
	<table>
		<tr>
			<td>Name</td>
			<td><input type="text" name="name"></td>
		</tr>
		<tr>
			<td>Model</td>
			<td><input type="text" name="model"></td>
		</tr>
		<tr>
			<td>Description</td>
			<td><input type="text" name="description"></td>
		</tr>
		<tr>
			<td>Manufacturer</td>
			<td><input type="text" name="manufacturer"></td>
		</tr>
		<tr>
			<td>Serial number</td>
			<td><input type="text" name="reg_no"></td>
		</tr>
		<tr>
			<td>Device type</td>
			<td>
				<select name="device_type">
		<%
					int j;
					for(j = 0; j < deviceTypeArray.length; j++) {
						DeviceType deviceType = deviceTypeArray[j];
						//deviceType.getDevice_type() == type_id &&
						if(deviceType.getLevel() == 1) {
		%>
							<option value="<%= deviceTypeArray[j].getDevice_type() %>" disabled><%= deviceTypeArray[j].getType_name() %></option>
		<%	
						} else if(deviceType.getLevel() == 2){
		%>
								<option value="<%= deviceTypeArray[j].getDevice_type() %>">...<%= deviceTypeArray[j].getType_name() %></option>	
		<%	
						}
					}
		%>
				</select>
			</td>
		</tr>
		</table>
	<input type="submit" value="Insert"/>
</form>
		<%
		}
	}
		%>
</body>
</html>